/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.m_ui.tmp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.opu.odg.dg.DocumentGeneraionService;
import org.opu.odg.dg.impl.ConnectionHolder;
import org.opu.odg.dg.impl.DocumentGeneraionServiceImpl;
import org.opu.odg.dg.impl.DocumentGenerationContext;
import org.opu.odg.dg.incomingdata.DatabaseSettings;
import org.opu.odg.dg.util.XmlSerializator;

/**
 *
 * @author yura
 */
public class BeanFactory {
    private ApplicationContext applicationContext;

    private ConnectionHolder connectionHolder;
    private DocumentGenerationContext documentGenerationContext;
    private TemplateRepositoryImpl templateRepository;
    private DocumentGeneraionService documentGeneraionService;
    private DatabaseSettings databaseSettings;

    public BeanFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public TemplateRepositoryImpl getTemplateRepository() {
        if (templateRepository == null) {
            File repository = new File(applicationContext.getProperty("repository"));
            File templateNames = new File(repository, applicationContext.getProperty("templateNames"));
            if (repository.exists()) {
                if (repository.isFile()) {
                    throw new IllegalArgumentException("The repository " +
                            repository + " must be a folder, not a file");
                }
            } else {
                repository.mkdirs();
            }

            try {
                templateRepository = new TemplateRepositoryImpl(repository, templateNames);
            } catch (IOException ex) {
                throw new IllegalArgumentException(ex);
            }
        }
        return templateRepository;
    }

    public DocumentGeneraionService getDocumentGeneraionService() {
        if (documentGeneraionService == null) {
            documentGeneraionService = new DocumentGeneraionServiceImpl(
                    getDocumentGenerationContext());
        }
        return documentGeneraionService;
    }

    public DocumentGenerationContext getDocumentGenerationContext() {
        if (documentGenerationContext == null) {
             documentGenerationContext = new DocumentGenerationContext(
                     getConnectionHolder(),
                     getTemplateRepository());
        }
        return documentGenerationContext;
    }

    public ConnectionHolder getConnectionHolder() {
        if (connectionHolder == null) {
            connectionHolder = new ConnectionHolder(getDatabaseSettings());
        }
        return connectionHolder;
    }

    public DatabaseSettings getDatabaseSettings() {
        if (databaseSettings == null) {
            InputStream stream = null;
            try {
                stream = new FileInputStream(getDefaultDatabaseSettingsFile());
                databaseSettings = XmlSerializator.readSafe(
                        DatabaseSettings.class, IOUtils.toByteArray(stream));
            } catch (Exception ex) {
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }
        return databaseSettings;
    }

    public File getDefaultDatabaseSettingsFile() {
        return new File(System.getProperty("user.home", ""),
                applicationContext.getProperty("defaultDatabaseSettings"));
    }
}
